package com.cooba.adapter;


import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cooba.R;
import com.cooba.bean.DrawOutRecordBean;
import com.cooba.bean.LivingDetailBean;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.Utils;
import com.cooba.views.GlideRoundTransform;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

public class LivingDetailAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private final int width;
    private final int height;
    private Context context;
    private List<LivingDetailBean.LivingDetailContent> list;

    public List<LivingDetailBean.LivingDetailContent> getList() {
        return list;
    }

    public void setList(List<LivingDetailBean.LivingDetailContent> list) {
        this.list = list;
        Log.d("wei", "" + list.size());
        this.notifyDataSetChanged();
    }

    public void addNewData(List<LivingDetailBean.LivingDetailContent> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<LivingDetailBean.LivingDetailContent> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public LivingDetailAdapter(Context context, List<LivingDetailBean.LivingDetailContent> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        /**
         * 设置默认的图片展现、加载失败的图片展现
         */
        bitmapUtils.configDefaultLoadingImage(R.mipmap.default_icon1);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.default_icon1);
        width = context.getResources().getDisplayMetrics().widthPixels;
        height =context.getResources().getDisplayMetrics().heightPixels;
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public LivingDetailBean.LivingDetailContent getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_my_living_detail_item, parent, false);
            holder.my_living__detail_item_message_bold_tv = (TextView) convertView.findViewById(R.id.my_living__detail_item_message_bold_tv);
            holder.my_living__detail_item_content_bold_ll = (LinearLayout) convertView.findViewById(R.id.my_living__detail_item_content_bold_ll);
            holder.my_living__detail_item_date = (TextView) convertView.findViewById(R.id.my_living__detail_item_date);
            holder.my_living__detail_item_content_normal_ll = (LinearLayout) convertView.findViewById(R.id.my_living__detail_item_content_normal_ll);
            holder.my_living__detail_item_message_normal_tv = (TextView) convertView.findViewById(R.id.my_living__detail_item_message_normal_tv);
            holder.my_living__detail_item_content_image_ll = (LinearLayout) convertView.findViewById(R.id.my_living__detail_item_content_image_ll);
            holder.my_living__detail_item_circle_iv= (ImageView) convertView.findViewById(R.id.my_living__detail_item_circle_iv);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        if (list.get(position).getMessageType().equals("0")) {  //0--文本 1--图片
            if (list.get(position).getIsTitle().equals("0")) {  //0--正常  1--加粗
                holder.my_living__detail_item_content_normal_ll.setVisibility(View.VISIBLE);
                holder.my_living__detail_item_content_bold_ll.setVisibility(View.GONE);
                holder.my_living__detail_item_content_image_ll.setVisibility(View.GONE);
                holder.my_living__detail_item_circle_iv.setImageResource(R.mipmap.content);
                holder.my_living__detail_item_message_normal_tv.setText(list.get(position).getMessage());
            } else {
                holder.my_living__detail_item_content_normal_ll.setVisibility(View.GONE);
                holder.my_living__detail_item_content_image_ll.setVisibility(View.GONE);
                holder.my_living__detail_item_content_bold_ll.setVisibility(View.VISIBLE);
                holder.my_living__detail_item_circle_iv.setImageResource(R.mipmap.title);
                holder.my_living__detail_item_message_bold_tv.setText(list.get(position).getMessage());
                holder.my_living__detail_item_date.setText(Utils.subStrTime(list.get(position).getCreateDate()));
            }
        }else {
            holder.my_living__detail_item_content_normal_ll.setVisibility(View.GONE);
            holder.my_living__detail_item_content_image_ll.setVisibility(View.VISIBLE);
            holder.my_living__detail_item_content_bold_ll.setVisibility(View.GONE);
            holder.my_living__detail_item_circle_iv.setImageResource(R.mipmap.image);
            String[] slit = list.get(position).getMessage().split("#");
            if (holder.my_living__detail_item_content_image_ll.getChildCount()!=0){
                holder.my_living__detail_item_content_image_ll.removeAllViewsInLayout();
            }

            String[] cover=list.get(position).getWidthHeight().split("#");
            for (int i=0;i<slit.length;i++){
               String[] cover2 = cover[i].split("\\*");
                int image_width=width-CommonUtil.dip2px(context,50);
                int image_height=image_width*Integer.parseInt(cover2[1])/Integer.parseInt(cover2[0]);

                ImageView iv = new ImageView(context);
                //iv.setImageResource(R.mipmap.default_icon1);

                iv.setLayoutParams(new LinearLayout.LayoutParams(image_width, image_height));
                //iv.setScaleType(ImageView.ScaleType.FIT_XY);
                iv.setPadding(0, 20, 0, 0);
                bitmapUtils.display(iv, slit[i]);
                //Glide.with(context).load(slit[i]).placeholder(R.mipmap.default_icon1).transform(new GlideRoundTransform(context, 10)).into(iv);
                holder.my_living__detail_item_content_image_ll.addView(iv);
            }
        }
        return convertView;
    }


    class ViewHolder {
        LinearLayout my_living__detail_item_content_bold_ll;//加粗文本布局
        TextView my_living__detail_item_message_bold_tv;
        TextView my_living__detail_item_date;

        LinearLayout my_living__detail_item_content_normal_ll;//正常文本布局
        TextView my_living__detail_item_message_normal_tv;

        LinearLayout my_living__detail_item_content_image_ll;//图片布局

        ImageView my_living__detail_item_circle_iv;

    }

}
